<template>
  <div>
    <!-- 搜索 -->
    <div class="search-form">
      <el-form :inline="true" :model="params" size="small">
        <el-form-item label="标题" label-width="80px">
          <el-input
            v-model="params.blogTitle"
            placeholder="请输入标题"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="文章来源" label-width="80px">
          <el-input
            v-model="params.blogSource"
            placeholder="请输入来源"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="分类" label-width="80px">
          <el-select
            v-model="params.blogType"
            placeholder="请求选择分类"
            clearable
          >
            <el-option
              v-for="item in typeList"
              :key="item.typeId"
              :label="item.typeName"
              :value="item.typeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="button-group">
      <el-button type="primary" size="small" @click="toAdd">添加</el-button>
    </div>
    <!-- 数据表格 -->
    <div class="data-table">
      <el-table
        :data="blogList"
        style="width: 100%"
        height="520"
        :header-cell-style="{ 'text-align': 'center' }"
        :cell-style="{ 'text-align': 'center' }"
      >
        <!-- <el-table-column prop="blogImage" label="封面" width="150px">
          <template slot-scope="scope">
            <el-image
              style="width: 80px; height: 80px"
              :src="scope.row.blogImage"
              fit="fill"
            ></el-image>
          </template>
        </el-table-column> -->
        <el-table-column prop="blogTitle" label="标题" width="180px">
        </el-table-column>
        <el-table-column prop="blogType" label="博客分类" width="120px">
        </el-table-column>
        <el-table-column prop="typeName" label="分类名称" width="120px">
        </el-table-column>
        <el-table-column prop="tagNameList" label="标签" width="180px">
          <template slot-scope="scope">
            <el-tag
              v-for="(item, index) in scope.row.tagNameList"
              :key="index"
              size="mini"
              class="blog-tag"
              >{{ item }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="blogRemark" label="简介" width="500px">
        </el-table-column>
        <el-table-column prop="blogSource" label="文章来源" width="300px">
        </el-table-column>
        <el-table-column label="数据统计" width="120px">
          <template slot-scope="scope">
            <div class="data-item">点赞数：{{ scope.row.blogGoods }}</div>
            <div class="data-item">阅读数：{{ scope.row.blogRead }}</div>
            <div class="data-item">收藏数：{{ scope.row.blogCollection }}</div>
            <div class="data-item">评论数：{{ scope.row.blogComment }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="createdTime" label="创建时间" width="100px">
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间" width="100px">
        </el-table-column>
        <el-table-column label="操作" width="150px" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="toInfo(scope.row.blogId)"
              >查看</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="toUpdate(scope.row.blogId)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="deleteById(scope.row.blogId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 数据表格结束 -->

    <!-- 添加弹窗 -->
    <el-dialog title="添加" :visible.sync="addDialog" width="70%" style="margin-bottom: 80px">
      <blog-add v-if="addDialog" @after="getByPage" @closeDialog="closeDialog" />
    </el-dialog>
    <!-- 添加弹窗结束 -->

    <!-- 修改弹窗 -->
    <!-- v-if="updateDialog" 每次打开弹窗都重新进行数据请求 -->
    <el-dialog title="编辑" :visible.sync="updateDialog" width="70%" style="margin-bottom: 80px">
      <blog-update
        v-if="updateDialog"   
        :active-id="activeId"
        @after="getByPage"
        @closeDialog="closeDialog"
      />
    </el-dialog>
    <!-- 修改弹窗结束 -->

    <!-- 详情弹窗 -->
    <el-dialog title="详情" :visible.sync="infoDialog" width="50%">
      <blog-info
        v-if="infoDialog"
        :active-id="activeId"
        @closeDialog="closeDialog"
      />
    </el-dialog>
    <!-- 详情弹窗结束 -->

    <!-- 分页组件 -->
    <div class="pageable-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="params.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
      >
      </el-pagination>
    </div>
    <!-- 分页组件结束 -->
  </div>
</template>

<script>
import { getByPage, deleteById } from "@/api/blog";
import blogAdd from "./blog-add";
import blogUpdate from "./blog-update";
import blogInfo from "./blog-info";
import { list } from "@/api/type";

export default {
  components: {
    blogAdd,
    blogUpdate,
    blogInfo,
  },
  data() {
    return {
      // 博客列表
      blogList: [],
      // 总条数
      count: 0,
      // 查询分页对象
      params: {
        page: 1,
        limit: 5,
        sortColumn: "created_time"
      },
      // 控制添加弹窗显示
      addDialog: false,
      updateDialog: false,
      infoDialog: false,
      // 当前点击的id
      activeId: null,
      // 分类列表
      typeList: [],
    };
  },
  created() {
    this.getByPage();
    this.getTypeList();
  },
  methods: {
    // 当前页条数发生改变时触碰
    handleSizeChange(val) {
      this.params.limit = val;
      this.getByPage();
    },
    // 当前页发生改变时触碰
    handleCurrentChange(val) {
      this.params.page = val;
      this.getByPage();
    },
    // 加载分类列表
    getTypeList() {
      list().then((res) => {
        this.typeList = res.data;
      });
    },
    // 查询
    search() {
      this.getByPage();
    },
    // 博客列表页
    getByPage() {
      getByPage(this.params).then((res) => {
        this.blogList = res.data;
        this.count = parseInt(res.count);
      });
    },

    // 打开添加博客弹窗
    toAdd() {
      this.addDialog = true;
    },

    // 打开修改博客弹窗
    toUpdate(data) {
      this.activeId = data;
      this.updateDialog = true;
    },

    // 打开详情弹窗
    toInfo(data) {
      this.activeId = data;
      this.infoDialog = true;
    },

    // 关闭弹窗
    closeDialog() {
      this.addDialog = false;
      this.updateDialog = false;
      this.infoDialog = false;
    },

    // 根据id删除
    deleteById(id) {
      this.$confirm("此操作将永久删除该条博客, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteById(id).then((res) => {
          this.$message.success(res.msg);
          this.getByPage();
        });
      });
    },
  },
};
</script>

<style scoped>
.blog-tag {
  margin: 3px 5px;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>